/**
 * @name         RepNET Home Screen
 * @version      1.0a
 * @author       Kasun Rathnayaka <kazar55@gmail.com>
 * @author       Ramindu Deshapriya <rasade88@gmail.com>
 * @author 		 Prabodha Dissanayake <prabodha89@gmail.com>
 * @author 		 Thusira Dissanayake <thusira14@gmail.com>
 * @about        Developed by MiDi
 * @license	 http://www.gnu.org/licenses/lgpl-2.1.html GNU Lesser General Public License (LGPL)
 * @lastModified 2012.09.24
 */
package com.midi.repnet.ui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTabbedPane;
import javax.swing.JLabel;
import java.awt.Font;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Iterator;

import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JCheckBox;
import javax.swing.JSeparator;
import javax.swing.JToolBar;
import javax.swing.JList;
import java.awt.Color;
import javax.swing.ImageIcon;
import javax.swing.JMenuBar;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;

import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;

import java.util.HashMap;

import java.util.List;
import java.util.ListIterator;

import javax.swing.UIManager;
import javax.swing.Icon;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import com.midi.repnet.main.addElements;
import javax.swing.JScrollPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.border.EtchedBorder;
import javax.swing.JScrollPane;

import com.midi.repnet.main.*;
import com.midi.repnet.models.*;
import com.mysql.jdbc.Statement;

import java.awt.Toolkit;

import javax.swing.border.Border;
import javax.swing.border.MatteBorder;
import javax.swing.border.TitledBorder;
import javax.swing.border.BevelBorder;
import javax.swing.border.LineBorder;
import javax.swing.SwingConstants;

public class HomeScreen extends JFrame {

	private JPanel contentPane;
	public static final JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);

	private JTextField be_billNo;
	private JTextField be_amnt_textField;
	private static JTextField ad_agency_textField;
	private static JTextField ad_region_textField;
	private static JTextField ad_shop_textField;
	private static JTextField ad_contact_textfield;
	private static JTextField cr_amount_textField;
	private static JTextField cr_chequeAmount_textField;
	private static JTextField cr_date_textField;
	private static JTextField cr_bank_textfield;
	private static JTextField cr_chequeNo_textField;
	private JTextField cc_date_textField;
	private static JTextField cc_dateChequeTextField;
	private JTable wh_table;

	private float wholesaleAmount = 0.0f;

	// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< karama code

	// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< karama code

	private static JTextArea ad_shop_textArea;
	private static JComboBox ad_region_comboBox;
	private JComboBox be_agency_comboBox = new JComboBox();
	private JComboBox be_region_comboBox = new JComboBox();
	private JComboBox be_shop_comboBox = new JComboBox();
	private final JComboBox cc_agency_comboBox = new JComboBox();
	private final JComboBox cc_region_comboBox = new JComboBox();
	private final JComboBox cc_shop_comboBox = new JComboBox();
	private final JComboBox cr_billNo_comboBox = new JComboBox();
	private final JComboBox cr_region_comboBox = new JComboBox();
	private final JComboBox cr_shop_comboBox = new JComboBox();
	private final JComboBox cr_agencycomboBox = new JComboBox();

	private JList agency_list;
	DefaultListModel modelAgency;
	static String ValAgency;
	ListSelectionListener listSelectionListenerAgency;

	private JList region_list;
	DefaultListModel modelRegion;
	static String ValRegion;

	private ArrayList agency;
	private ArrayList region;
	private ArrayList shop;
	
	
	private JList shop_list;
	DefaultListModel modelShop;
	static String ValShop;
	private int addElementTabClickCount;
	private int billEntryTabClickCount;
	private int wholesaleTabClickCount;
	private int reportsTabClickCount;
	private int remitTabClickCount;
	private Shop cr_selectedShop;

	// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					HomeScreen frame = new HomeScreen();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public HomeScreen() {
		setResizable(false);
		setIconImage(Toolkit.getDefaultToolkit().getImage(HomeScreen.class.getResource("/com/midi/repnet/resources/icon.jpg")));
		final JFrame home = this;
		setTitle("RepNET v1.0 - Lathpandura Holdings");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 538, 694);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		tabbedPane.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				int tabId = tabbedPane.getSelectedIndex();
				System.out.println(tabId);

				switch (tabId) {

				case 0:
					// Bill entry load
					be_agency_comboBox.removeAllItems();
					be_agency_comboBox.insertItemAt("Choose Agency", 0);
					be_agency_comboBox.setSelectedIndex(0);

					be_region_comboBox.removeAllItems();
					be_region_comboBox.insertItemAt("Choose Region", 0);
					be_region_comboBox.setSelectedIndex(0);

					be_shop_comboBox.removeAllItems();
					be_shop_comboBox.insertItemAt("Choose Shop", 0);
					be_shop_comboBox.setSelectedIndex(0);

					ArrayList<Agency> agencyList = Common.loadAgencies();
					for (Agency agency : agencyList) {
						be_agency_comboBox.addItem(agency);
					}

					ArrayList<Region> regions = Common.loadRegions();
					for (Region region : regions) {
						be_region_comboBox.addItem(region);
					}

					Common.shops = Common.loadShops("%");
					for (Shop shop : Common.shops) {
						be_shop_comboBox.addItem(shop);
					}
					System.out.println("loaded..............");
					break;

				case 1:
					//Credit Remittance Load
					cr_agencycomboBox.removeAllItems();
					cr_agencycomboBox.insertItemAt("Choose Agency", 0);
					cr_agencycomboBox.setSelectedIndex(0);
					
					cr_region_comboBox.removeAllItems();
					cr_region_comboBox.insertItemAt("Choose Region", 0);
					cr_region_comboBox.setSelectedIndex(0);
					
					cr_shop_comboBox.removeAllItems();
					cr_shop_comboBox.insertItemAt("Choose Shop", 0);
					cr_shop_comboBox.setSelectedIndex(0);
					
					ArrayList<Agency> cr_agencies = Common.loadAgencies();
					for ( Agency agency: cr_agencies ) {
						cr_agencycomboBox.addItem(agency);
					}
					
					ArrayList<Region> cr_regions = Common.loadRegions();
					for ( Region region: cr_regions ) {
						cr_region_comboBox.addItem(region);
					}
					
					ArrayList<Shop> cr_shops = Common.loadShops("%");
					for ( Shop shop: cr_shops ) {
						cr_shop_comboBox.addItem(shop);
					}
					clearCreditRemittanceFields();
					break;
//				case 2:
//					// Wholesale load
//					wh_region_cmb.removeAllItems();
//					wh_region_cmb.insertItemAt("Choose Region", 0);
//					wh_region_cmb.setSelectedIndex(0);
//
//					wh_shop_cmb.removeAllItems();
//					wh_shop_cmb.insertItemAt("Choose Shop", 0);
//					wh_shop_cmb.setSelectedIndex(0);
//
//					ArrayList<Region> wh_regions = Common.loadRegions();
//					for (Region region : wh_regions) {
//						wh_region_cmb.addItem(region);
//					}
//
//					Common.shops = Common.loadShops("%");
//					for (Shop shop : Common.shops) {
//						wh_shop_cmb.addItem(shop);
//					}
//					System.out.println("WH_loaded..............");
//
//					break;
				case 2:

					// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< karama code
					int id=1;
					boolean check=PasswordChecker.checkResult(id);
				   // Uses boolean to determine if password is valid
				   if(check==true)
				       {
				       //JOptionPane.showMessageDialog(null, "Welcome to Facebook");
						if (addElementTabClickCount == 0) {
							// Load Data needed to append to the lists in
							// AddElements Tab
							Object[] listObject = addElements
									.loadDataAddElementsFrame();

							 agency = (ArrayList) listObject[0];
							 region = (ArrayList) listObject[1];
							   shop = (ArrayList) listObject[2];
							   
//							   for (int i = 0; i < shop.size(); i++) {
//								
//								   System.out.println("@@@@@@@@"+shop.get(i));
//							}
							   

							// Add data to agencyList
							Iterator itr = agency.iterator();
							int count = 0;
							while (itr.hasNext()) {

								modelAgency.add(count, itr.next());
								count++;

							}
							// Add data to regionList
							Iterator itrOne = region.iterator();
							int countOne = 0;
							String returnRegion = null;
							while (itrOne.hasNext()) {

								returnRegion = (String) itrOne.next();
								if(!(returnRegion.equalsIgnoreCase("Uncategorized"))){
								modelRegion.add(countOne, returnRegion);
								ad_region_comboBox.insertItemAt(returnRegion,
										countOne);
								countOne++;
								}
							}

							// Add data to shopList
							
							ArrayList releventRegion= new ArrayList ();
							
							
							
							
							Iterator itrTwo = shop.iterator();
							Iterator itrThree=null;
							String listValue="";
							String shopName="";
							int countTwo = 0;
							while (itrTwo.hasNext()) {
								shopName=itrTwo.next().toString();
								System.out.println("@@@@@@@@"+shopName);
								String setValue=shopName+"-"+addElements.getRegionsMapToShop(shopName);
								modelShop.add(countTwo, setValue);
								countTwo++;
								//releventRegion=addElements.getRegionsMapToShop(shopName);
								//itrThree=releventRegion.iterator();
								
//								while(itrThree.hasNext()){
//									listValue=shopName+"-"+itrThree.next().toString();
//									
//									modelShop.add(countTwo, listValue);
//									countTwo++;
//									
//								}
								
								
								

							}
							addElementTabClickCount = 1;
						}
					   
				       }
				   else
				       {
				       JOptionPane.showMessageDialog(null, "Enter Correct Password");
				       tabbedPane.setSelectedIndex(0);
				       }
				   
					
			

					break;
				// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
				case 3:
					// Reports load

					cc_agency_comboBox.removeAllItems();
					cc_agency_comboBox.insertItemAt("Choose Agency", 0);
					cc_agency_comboBox.setSelectedIndex(0);

					cc_region_comboBox.removeAllItems();
					cc_region_comboBox.insertItemAt("Choose Region", 0);
					cc_region_comboBox.setSelectedIndex(0);

					cc_shop_comboBox.removeAllItems();
					cc_shop_comboBox.insertItemAt("Choose Shop", 0);
					cc_shop_comboBox.setSelectedIndex(0);

					ArrayList<Agency> cc_agencyList = Common.loadAgencies();
					for (Agency agency : cc_agencyList) {
						cc_agency_comboBox.addItem(agency);
					}

					ArrayList<Region> cc_regions = Common.loadRegions();
					for (Region region : cc_regions) {
						cc_region_comboBox.addItem(region);
					}

					Common.shops = Common.loadShops("%");
					for (Shop shop : Common.shops) {
						cc_shop_comboBox.addItem(shop);
					}

					break;
				default:
					break;
				}

			}

			
		});

		tabbedPane.setBounds(10, 11, 511, 634);
		contentPane.add(tabbedPane);

		// ////////////////////////////////BILL
		// ENTRY////////////////////////////////////////
		/**
		 * Variable Naming convention: be_<variable_name>
		 */

		JPanel billEntryPanel = new JPanel();
		tabbedPane.addTab("Bill Entry", (Icon) null, billEntryPanel, null);
		billEntryPanel.setLayout(null);

		JLabel label_4 = new JLabel("Credit Collection");
		label_4.setBounds(58, -38, 163, 38);
		label_4.setFont(new Font("Tahoma", Font.PLAIN, 20));
		billEntryPanel.add(label_4);

		JLabel label_6 = new JLabel("Bill Entry");
		label_6.setFont(new Font("Tahoma", Font.PLAIN, 20));
		label_6.setBounds(10, 33, 92, 26);
		billEntryPanel.add(label_6);

		JLabel lblBillNo = new JLabel("Bill No :");
		lblBillNo.setBounds(108, 96, 46, 14);
		billEntryPanel.add(lblBillNo);

		JLabel lblAgency = new JLabel("Agency :");
		lblAgency.setBounds(108, 146, 53, 14);
		billEntryPanel.add(lblAgency);

		JLabel lblRegion = new JLabel("Region :");
		lblRegion.setBounds(108, 201, 46, 14);
		billEntryPanel.add(lblRegion);

		JLabel lblShop = new JLabel("Shop :");
		lblShop.setBounds(108, 263, 46, 14);
		billEntryPanel.add(lblShop);

		JLabel lblDiscription = new JLabel("Description :");
		lblDiscription.setBounds(108, 328, 64, 14);
		billEntryPanel.add(lblDiscription);

		JLabel lblAmount = new JLabel("Amount :");
		lblAmount.setBounds(108, 481, 53, 14);
		billEntryPanel.add(lblAmount);

		be_billNo = new JTextField();
		be_billNo.setColumns(10);
		be_billNo.setBounds(186, 93, 199, 20);
		billEntryPanel.add(be_billNo);

		be_region_comboBox.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {

				if (be_region_comboBox.getSelectedItem() != null) {
					if (be_region_comboBox.getSelectedItem().toString()
							.equals("Choose Region")) {
						Common.shops = Common.loadShops("%");

					} else {
						Common.shops = Common.loadShops(be_region_comboBox
								.getSelectedItem().toString());
					}
					be_shop_comboBox.removeAllItems();
					be_shop_comboBox.insertItemAt("Choose Shop", 0);
					be_shop_comboBox.setSelectedIndex(0);
					for (Shop shop : Common.shops) {
						be_shop_comboBox.addItem(shop);
					}
				}

			}
		});

		be_agency_comboBox.setBounds(186, 143, 199, 20);
		billEntryPanel.add(be_agency_comboBox);

		be_region_comboBox.setBounds(186, 198, 199, 20);
		billEntryPanel.add(be_region_comboBox);

		be_shop_comboBox.setBounds(186, 260, 199, 20);
		billEntryPanel.add(be_shop_comboBox);

		be_amnt_textField = new JTextField();
		be_amnt_textField.setColumns(10);
		be_amnt_textField.setBounds(186, 478, 199, 20);
		billEntryPanel.add(be_amnt_textField);

		JLabel label_13 = new JLabel("Rs.");
		label_13.setBounds(156, 481, 32, 14);
		billEntryPanel.add(label_13);

		final JTextArea be_des_textArea = new JTextArea();
		Border border = BorderFactory.createLineBorder(Color.lightGray);		
		be_des_textArea.setBorder(border);		
		be_des_textArea.setLineWrap(true);
		be_des_textArea.setBackground(Color.WHITE);
		be_des_textArea.setBounds(186, 323, 199, 107);
		billEntryPanel.add(be_des_textArea);

		JButton be_submitBtn = new JButton("Submit");
		be_submitBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {

				String new_billNo = be_billNo.getText();
				Object new_agency = be_agency_comboBox.getSelectedItem();
				Object new_region = be_region_comboBox.getSelectedItem();
				Object new_shop = be_shop_comboBox.getSelectedItem();
				String new_description = be_des_textArea.getText();

				if (new_billNo.equals("") || new_agency == null
						|| new_region == null || new_shop == null
						|| new_shop.equals("Choose Shop")
						|| new_agency.equals("Choose Agency")
						|| new_region.equals("Choose Region")
						|| new_description.equals("")
						|| be_amnt_textField.getText().equals("")) {
					JOptionPane.showMessageDialog(null,
							"Please enter all fields...");
				} else {
					try {						
						float new_amount = Float.parseFloat(be_amnt_textField.getText());
						Agency newAgency=(Agency) new_agency;						
						Shop newShop = (Shop)new_shop;						
						boolean IsRecorded=new BillEntry().submitData(new_billNo, newAgency.getId(),newShop.getId(), new_description, new_amount);
						
						if(IsRecorded){
							JOptionPane.showMessageDialog(null, "Bill Entry Added Successfully.");
							be_billNo.setText("");
							be_amnt_textField.setText("");
							be_des_textArea.setText("");
							be_agency_comboBox.setSelectedIndex(0);
							be_region_comboBox.setSelectedIndex(0);
							be_shop_comboBox.setSelectedIndex(0);
						}
						
						
					} catch (NumberFormatException nfe) {
						JOptionPane.showMessageDialog(null, "Invalid Format...");
					}
				}
			}
		});
		be_submitBtn.setBounds(296, 537, 89, 23);
		billEntryPanel.add(be_submitBtn);

		// ////////////////////////////CREDIT
		// COLLECTION/////////////////////////////////////
		/**
		 * Variable Naming convention: cc_<variable_name>
		 */

		// /////////////////////////////////CREDIT
		// REMIT///////////////////////////////////////////
		/**
		 * Variable Naming convention: cr_<variable_name>
		 */

		JPanel creditRemitPanel = new JPanel();
		tabbedPane.addTab("Remit", null, creditRemitPanel, null);
		tabbedPane.setEnabledAt(1, true);
		creditRemitPanel.setLayout(null);

		JLabel label_14 = new JLabel("Bill No: ");
		label_14.setBounds(106, 245, 67, 14);
		creditRemitPanel.add(label_14);

		JLabel lblAgency_1 = new JLabel("Agency:");
		lblAgency_1.setBounds(106, 106, 67, 14);
		creditRemitPanel.add(lblAgency_1);

		JLabel lblRegion_1 = new JLabel("Region:");
		lblRegion_1.setBounds(106, 149, 67, 14);
		creditRemitPanel.add(lblRegion_1);

		JLabel lblShop_1 = new JLabel("Shop:");
		lblShop_1.setBounds(106, 201, 67, 14);
		creditRemitPanel.add(lblShop_1);

		JLabel label_18 = new JLabel("Payment Method");
		label_18.setFont(new Font("Tahoma", Font.PLAIN, 20));
		label_18.setBounds(10, 348, 183, 23);
		creditRemitPanel.add(label_18);

		final JCheckBox cr_checkBoxCash = new JCheckBox("Cash");
		cr_checkBoxCash.setBounds(34, 394, 97, 23);
		creditRemitPanel.add(cr_checkBoxCash);

		final JCheckBox cr_checkBoxCheque = new JCheckBox("Cheque");
		cr_checkBoxCheque.setBounds(263, 397, 97, 23);
		creditRemitPanel.add(cr_checkBoxCheque);

		JLabel lblAmount_1 = new JLabel("Amount:");
		lblAmount_1.setBounds(34, 467, 46, 14);
		creditRemitPanel.add(lblAmount_1);

		cr_amount_textField = new JTextField();
		cr_amount_textField.setColumns(10);
		cr_amount_textField.setBounds(90, 464, 103, 20);
		creditRemitPanel.add(cr_amount_textField);

		JLabel label_20 = new JLabel("Cheque No:");
		label_20.setBounds(263, 470, 75, 14);
		creditRemitPanel.add(label_20);

		JLabel lblBank = new JLabel("Bank:");
		lblBank.setBounds(263, 495, 75, 14);
		creditRemitPanel.add(lblBank);

		JLabel lblDate = new JLabel("Date:");
		lblDate.setBounds(263, 520, 75, 14);
		creditRemitPanel.add(lblDate);

		JLabel lblAmount_2 = new JLabel("Amount:");
		lblAmount_2.setBounds(263, 548, 46, 14);
		creditRemitPanel.add(lblAmount_2);

		cr_chequeAmount_textField = new JTextField();
		cr_chequeAmount_textField.setColumns(10);
		cr_chequeAmount_textField.setBounds(335, 545, 128, 20);
		creditRemitPanel.add(cr_chequeAmount_textField);

		cr_date_textField = new JTextField();
		cr_date_textField.setEditable(false);
		cr_date_textField.setColumns(10);
		cr_date_textField.setBounds(335, 517, 97, 20);
		creditRemitPanel.add(cr_date_textField);

		cr_bank_textfield = new JTextField();
		cr_bank_textfield.setColumns(10);
		cr_bank_textfield.setBounds(335, 492, 128, 20);
		creditRemitPanel.add(cr_bank_textfield);

		cr_chequeNo_textField = new JTextField();
		cr_chequeNo_textField.setColumns(10);
		cr_chequeNo_textField.setBounds(335, 467, 128, 20);
		creditRemitPanel.add(cr_chequeNo_textField);

		
		cr_agencycomboBox.addItem("Choose Agency");
		ArrayList<Agency> agencyList = Common.loadAgencies();
		for (Agency agency : agencyList) {
			cr_agencycomboBox.addItem(agency);
		}
		cr_agencycomboBox.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				if ( cr_shop_comboBox.getItemCount() > 0 && cr_agencycomboBox.getItemCount() > 0 ) {
					if (cr_agencycomboBox.getSelectedItem().toString().equals("Choose Agency")
							&& cr_shop_comboBox.getSelectedItem().toString().equals("Choose Shop")) {
						Common.bills = Common.loadBills("%", "%");
					} else if (cr_shop_comboBox.getSelectedItem().toString().equals("Choose Shop")) {
						Common.bills = Common.loadBills("%", cr_agencycomboBox.getSelectedItem().toString());
					} else if (cr_agencycomboBox.getSelectedItem().toString().equals("Choose Agency")) {
						Shop shop = (Shop)cr_shop_comboBox.getSelectedItem();
						Common.bills = Common.loadBills(shop.getName(), "%");
					} else {
						Shop shop = (Shop)cr_shop_comboBox.getSelectedItem();
						Common.bills = Common.loadBills(shop.getName(), 
								cr_agencycomboBox.getSelectedItem().toString());
					}
				}
				else {
					Common.bills = Common.loadBills("%", "%");
				}
				cr_billNo_comboBox.removeAllItems();
				cr_billNo_comboBox.addItem("Choose Bill");
				for (String bill : Common.bills) {
					cr_billNo_comboBox.addItem(bill);
				}

			}

		});
		cr_agencycomboBox.setBounds(183, 103, 175, 20);
		creditRemitPanel.add(cr_agencycomboBox);

		cr_region_comboBox.addItem("Choose Region");
		ArrayList<Region> regions = Common.loadRegions();
		for (Region region : regions) {
			cr_region_comboBox.addItem(region);
		}
		cr_region_comboBox.setBounds(183, 146, 175, 20);
		cr_region_comboBox.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				if ( cr_region_comboBox.getItemCount() > 0 ) {
					cr_shop_comboBox.removeAllItems();
					// System.out.println("Region Selected: "+cr_region_comboBox.getSelectedItem().toString());
					if (cr_region_comboBox.getSelectedItem().toString().equals("Choose Region")) {
						Common.shops = Common.loadShops("%");
	
					} else {
						Common.shops = Common.loadShops(cr_region_comboBox.getSelectedItem().toString());
					}
	
					cr_shop_comboBox.addItem("Choose Shop");
					for (Shop shop : Common.shops) {
						cr_shop_comboBox.addItem(shop);
					}
				}

			}

		});
		creditRemitPanel.add(cr_region_comboBox);
		if (Common.shops == null) {
			ArrayList<Shop> shops = new ArrayList<Shop>();
			shops = Common.loadShops("%");
			cr_shop_comboBox.addItem("Choose Shop");
			for (Shop shop : shops) {
				// System.out.println(shop);
				cr_shop_comboBox.addItem(shop);
			}
			cr_shop_comboBox.setBounds(183, 198, 175, 20);
			creditRemitPanel.add(cr_shop_comboBox);
		}
		cr_shop_comboBox.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				if ( cr_shop_comboBox.getItemCount() > 0 && cr_agencycomboBox.getItemCount() > 0 ) {
					if (cr_agencycomboBox.getSelectedItem().toString().equals("Choose Agency")
							&& cr_shop_comboBox.getSelectedItem().toString().equals("Choose Shop")) {
						Common.bills = Common.loadBills("%", "%");
					} else if (cr_agencycomboBox.getSelectedItem().toString().equals("Choose Agency")) {
						Shop shop = (Shop)cr_shop_comboBox.getSelectedItem();
						Common.bills = Common.loadBills(shop.getName(), "%");
					} else if (cr_shop_comboBox.getSelectedItem().toString().equals("Choose Shop")) {
						Common.bills = Common.loadBills("%", cr_agencycomboBox.getSelectedItem().toString());
					} else {
						cr_selectedShop = (Shop)cr_shop_comboBox.getSelectedItem();
						Common.bills = Common.loadBills(cr_selectedShop.getName(),
								cr_agencycomboBox.getSelectedItem().toString());
					}
					cr_billNo_comboBox.removeAllItems();
					cr_billNo_comboBox.addItem("Choose Bill");
					for (String bill : Common.bills) {
						cr_billNo_comboBox.addItem(bill);
					}
				} 

			}

		});
		cr_shop_comboBox.setBounds(183, 198, 175, 20);
		creditRemitPanel.add(cr_shop_comboBox);

		if (Common.bills == null) {
			ArrayList<String> bills = new ArrayList<String>();
			bills = Common.loadBills("%", "%");
			cr_billNo_comboBox.addItem("Choose Bill");
			for (String bill : bills) {
				// System.out.println(shop);
				cr_billNo_comboBox.addItem(bill);
			}
			cr_billNo_comboBox.setBounds(183, 198, 175, 20);
			creditRemitPanel.add(cr_billNo_comboBox);
		}
		cr_billNo_comboBox.setBounds(183, 242, 175, 20);
		creditRemitPanel.add(cr_billNo_comboBox);

		JSeparator separator = new JSeparator();
		separator.setBounds(10, 317, 476, 2);
		creditRemitPanel.add(separator);

		JButton buttonCalendar = new JButton("");
		buttonCalendar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				cr_date_textField.setText(new DatePicker(home).setPickedDate());
			}
		});
		buttonCalendar.setIcon(new ImageIcon(HomeScreen.class
				.getResource("/com/midi/repnet/resources/date.gif")));
		buttonCalendar.setBounds(442, 516, 21, 23);
		creditRemitPanel.add(buttonCalendar);

		JButton buttonSubmit = new JButton("Submit");
		buttonSubmit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				HashMap<String, String> cheque = new HashMap<String, String>();
				float cashAmount = 0.0f;
				String billNo = null;
				if ( !cr_checkBoxCheque.isSelected() && !cr_checkBoxCash.isSelected() ){
					JOptionPane.showMessageDialog(null, "You must select either Cash or Cheque");
				} else {
					if (cr_checkBoxCheque.isSelected()) {
						if (cr_chequeAmount_textField.getText().equals("")
								|| cr_bank_textfield.getText().equals("")
								|| cr_chequeNo_textField.getText().equals("")
								|| cr_date_textField.getText().equals("")) {
							JOptionPane.showMessageDialog(null,
									"Please enter all fields for cheque");
							cheque = null;
							
						} else {
							cheque.put("cheque_no", cr_chequeNo_textField.getText());
							cheque.put("amount",
									cr_chequeAmount_textField.getText());
							cheque.put("date", cr_date_textField.getText());
							cheque.put("bank", cr_bank_textfield.getText());
						}
	
					} else {
						cheque = null;
					}
					if (cr_checkBoxCash.isSelected()) {
						if (cr_amount_textField.getText().equals("")) {
							JOptionPane.showMessageDialog(null,"Please enter cash amount");
						} else {
							cashAmount = new Float(cr_amount_textField.getText());
						}
					}
					billNo = cr_billNo_comboBox.getSelectedItem().toString();
					if ( cashAmount > 0.0 || cheque != null ) {
						if (!CreditRemittance.submitData(billNo, cashAmount, cheque)) {
							JOptionPane.showMessageDialog(null,"Bill update unsucessful");
						}
						else {
							JOptionPane.showMessageDialog(null, "Bill update successful");
							clearCreditRemittanceFields();
						}
					}
				}
			}
		});
		buttonSubmit.setBounds(374, 576, 89, 23);
		creditRemitPanel.add(buttonSubmit);

		JLabel lblCreditRemittance = new JLabel("Credit Remittance");
		lblCreditRemittance.setFont(new Font("Tahoma", Font.PLAIN, 20));
		lblCreditRemittance.setBounds(10, 32, 175, 26);
		creditRemitPanel.add(lblCreditRemittance);

//		String col[] = { "Product", "Units", "Amount" };
//		final DefaultTableModel model = new DefaultTableModel(null, col) {
//			public boolean isCellEditable(int row, int column) {
//				return false;// This causes all cells to be not editable
//			}
//		};
//		header.setBackground(Color.yellow);
		
		// end prabodha
		// ///////////////////////////////ADD//
		// ELEMENTS/////////////////////////////////////////////
		/**
		 * Variable Naming convention: ad_<variable_name>
		 */

		JPanel addElementPanel = new JPanel();
		tabbedPane.addTab("Add Elements", null, addElementPanel, null);
		addElementPanel.setLayout(null);

		JList list = new JList();
		list.setBounds(323, 124, -51, 4);
		addElementPanel.add(list);

		JPanel panel_4 = new JPanel();
		panel_4.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		panel_4.setLayout(null);
		panel_4.setBounds(10, 11, 485, 149);
		addElementPanel.add(panel_4);

		JLabel lblAgencyName = new JLabel("Agency Name :");
		lblAgencyName.setBounds(10, 63, 77, 14);
		panel_4.add(lblAgencyName);

		ad_agency_textField = new JTextField();
		ad_agency_textField.setColumns(10);
		ad_agency_textField.setBounds(97, 60, 126, 20);
		panel_4.add(ad_agency_textField);

		JButton ad_agencyAddBtn = new JButton("Add");

		ad_agencyAddBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {

				// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< karama code
				// add new Agency to the System
				String newAgency = ad_agency_textField.getText();

				
				boolean updateDB=true;
				Iterator itr = agency.iterator();
	
				//check for the duplicate values in the list
			
				if(!newAgency.trim().equals("")){
					while (itr.hasNext()) {
						String returnVal=itr.next().toString();
						if(newAgency.equalsIgnoreCase(returnVal)){
							updateDB=false;
							
						}

					}
					
					if(updateDB){
						
						DefaultListModel newAgencyModel = (DefaultListModel) agency_list
								.getModel();
						newAgencyModel.addElement(newAgency);
						agency_list.setModel(newAgencyModel);
						addElements.editAgency(newAgency, "add");
						clearFields();
						
						//update the agency arraylist after insertion of new element
						int sizeOfArray=newAgencyModel.getSize();
						//System.out.println(newAgencyModel.elementAt(0));
					    agency.clear();
						//int j=1;
						for (int i = 0; i < sizeOfArray; i++) {
							System.out.println(newAgencyModel.elementAt(i));
						agency.add(newAgencyModel.elementAt(i));
						//j++;
						}
						
					}
				
					else{
					
						JOptionPane.showMessageDialog(null, "Duplicate value please Enter a different Name"); 
						ad_agency_textField.setText("");	
					
			
					}
				}
				else{  
					JOptionPane.showMessageDialog(null, "Please Enter a value Before Add"); 
					ad_agency_textField.setText("");	

				}
				
		
		  
			}
		});
		ad_agencyAddBtn.setBounds(161, 84, 62, 23);
		panel_4.add(ad_agencyAddBtn);

		modelAgency = new DefaultListModel();

		// add new listener to the agency list
		listSelectionListenerAgency = new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent listSelectionEvent) {
				boolean adjust = listSelectionEvent.getValueIsAdjusting();
				// System.out.println(", Adjusting? " + adjust);
				if (!adjust) {
					JList list = (JList) listSelectionEvent.getSource();
					int selections[] = list.getSelectedIndices();
					Object selectionValues[] = list.getSelectedValues();
					for (int i = 0, n = selections.length; i < n; i++) {
						if (i == 0) {
						}
						ValAgency = (String) selectionValues[i];
						System.out.println(" Selections valAgency: "
								+ ValAgency);
					}
				}
			}
		};
		JButton ad_agencyRemoveBtn = new JButton("Remove");
		ad_agencyRemoveBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {

				// check if selection value from the list is not null
				if (ValAgency != null) {

					DefaultListModel newAgencyModel = (DefaultListModel) agency_list
							.getModel();
					
					if(Common.updateTransactionsOnAgencyDelete(ValAgency)){
					
					newAgencyModel.removeElement(agency_list.getSelectedValue());
					agency_list.setModel(newAgencyModel);
					addElements.editAgency(ValAgency, "remove");
					
					//update the agency arraylist
					//set agency arraylist with the values in the list model
					
					
					int sizeOfArray=newAgencyModel.getSize();
					//System.out.println(newAgencyModel.elementAt(0));
				    agency.clear();
					//int j=1;
					for (int i = 0; i < sizeOfArray; i++) {
						System.out.println(newAgencyModel.elementAt(i));
					agency.add(newAgencyModel.elementAt(i));
					//j++;
					}
					//String [] newAgencyArr =(String[]) newAgencyModel.toArray();
					//Collections.addAll(agency, newAgencyArr); 
					}
					else{
						JOptionPane.showMessageDialog(null, "You cannot delete this Agency"); 
						//ad_region_textField.setText("");	
					
						
					}
					
				}
			}
		});
		// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
		ad_agencyRemoveBtn.setBounds(389, 115, 86, 23);
		panel_4.add(ad_agencyRemoveBtn);

		JLabel label_25 = new JLabel("Add Agency");
		label_25.setFont(new Font("Tahoma", Font.PLAIN, 20));
		label_25.setBounds(10, 0, 126, 52);
		panel_4.add(label_25);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(235, 11, 238, 96);
		panel_4.add(scrollPane);
												
														// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< karama code
														agency_list = new JList(modelAgency);
														scrollPane.setViewportView(agency_list);
														agency_list.setFont(new Font("Arial", Font.BOLD, 11));
														agency_list.setBorder(new TitledBorder(null, "Agency List", TitledBorder.LEADING, TitledBorder.TOP, null, null));
														agency_list.setBackground(UIManager.getColor("Button.highlight"));
		agency_list.addListSelectionListener(listSelectionListenerAgency);
		// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
		JPanel panel_5 = new JPanel();
		panel_5.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		panel_5.setLayout(null);
		panel_5.setBounds(10, 171, 485, 183);
		addElementPanel.add(panel_5);

		JLabel label_26 = new JLabel("Add Region");
		label_26.setFont(new Font("Tahoma", Font.PLAIN, 20));
		label_26.setBounds(10, 0, 126, 52);
		panel_5.add(label_26);

		JLabel lblRegionName = new JLabel("Region Name:");
		lblRegionName.setBounds(10, 69, 77, 14);
		panel_5.add(lblRegionName);

		ad_region_textField = new JTextField();
		ad_region_textField.setColumns(10);
		ad_region_textField.setBounds(97, 66, 126, 20);
		panel_5.add(ad_region_textField);

		JButton ad_regionAddBtn = new JButton("Add");
		ad_regionAddBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {

				// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< karama code

				// check for null values before adding
				// add new Region to the System
				String newRegion = ad_region_textField.getText();


				boolean updateDB=true;
				Iterator itr = region.iterator();
				
				if(!newRegion.trim().equals("")){
				
					while (itr.hasNext()) {
						String returnVal=itr.next().toString();
						if(newRegion.equalsIgnoreCase(returnVal)){
							updateDB=false;
							
						}

					}
					if(updateDB){
					
				DefaultListModel newRegionModel = (DefaultListModel) region_list
						.getModel();
				newRegionModel.addElement(newRegion);
				region_list.setModel(newRegionModel);
				addElements.editRegion(newRegion, "add");
				ad_region_comboBox.addItem(newRegion);
				clearFields();
				
				//update the agency arraylist after insertion of new element
				int sizeOfArray=newRegionModel.getSize();
				//System.out.println(newAgencyModel.elementAt(0));
			    region.clear();
				//int j=1;
				for (int i = 0; i < sizeOfArray; i++) {
					System.out.println(newRegionModel.elementAt(i));
				region .add(newRegionModel.elementAt(i));
				//j++;
				}
				
					}
					else{
						
						JOptionPane.showMessageDialog(null, "Duplicate value please Enter a different Name"); 
						ad_region_textField.setText("");	
					
						
					}
				
				
			  }
				else{  
					JOptionPane.showMessageDialog(null, "Please Enter a value Before Add"); 
					ad_region_textField.setText("");	

				}
			}
		});
		ad_regionAddBtn.setBounds(161, 97, 62, 23);
		panel_5.add(ad_regionAddBtn);

		modelRegion = new DefaultListModel();

		ListSelectionListener listSelectionListenerRegion = new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent listSelectionEvent) {
				boolean adjust = listSelectionEvent.getValueIsAdjusting();
				if (!adjust) {
					JList list = (JList) listSelectionEvent.getSource();
					int selections[] = list.getSelectedIndices();
					Object selectionValues[] = list.getSelectedValues();
					for (int i = 0, n = selections.length; i < n; i++) {
						if (i == 0) {
						}
						ValRegion = (String) selectionValues[i];
						System.out.println(" Selections valRegion: "
								+ ValRegion);
					}
				}
			}
		};
		// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
		JButton ad_regionRemoveBtn = new JButton("Remove");
		ad_regionRemoveBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< karama code
				// check if selection value from the list is not null
				if (ValRegion != null) {

					DefaultListModel newRegionModel = (DefaultListModel) region_list
							.getModel();
					String selected=region_list.getSelectedValue().toString();
					newRegionModel.removeElement(region_list.getSelectedValue());
					region_list.setModel(newRegionModel);
					

					// get the items needed for combolist (onLoad)
					ad_region_comboBox.removeAllItems();
					
					
					
					int count = 0;
					int val=0;
					int index= 0;
					int firstOne=0;
					String next=null;
					DefaultListModel newshops = (DefaultListModel) shop_list.getModel();
					String returnOfModel=null;
					int lengthOfModel=newshops.getSize();
				ArrayList removeElements=new ArrayList();
				
				addElements.deleteShopMapToRegion(selected);
				System.out.println("@@@@@@"+selected);
				addElements.editRegion(ValRegion, "remove");
				ArrayList newRegionList = addElements.getAllRegions();
				Iterator itr = newRegionList.iterator();
				
				ArrayList newShopList = new ArrayList();
				newShopList=addElements.getNewList();
				
				
				DefaultListModel latestShop=new DefaultListModel();
				
				for (int j = 0; j < newShopList.size(); j++) {
					
					latestShop.add(j, newShopList.get(j));
					
				}
				
				
				
				
			//when we delete a region update the shop table
		   //In shop table delete records which have deleted region_ID
				
				
				
				
//				returnOfModel=newshops.getElementAt(0).toString();
//				System.out.println("returns are :"+returnOfModel);
//				//newshops.removeElement(newshops.getElementAt(0).toString());
//				removeElements.add(returnOfModel);
//				System.out.println("returns are :"+newshops.getElementAt(1).toString());
//				System.out.println("returns are :"+newshops.getElementAt(2).toString());
//			
			
				//check this code-------------------------------------------------
				
				
//					for (int i = 0; i <= lengthOfModel; i++) {
//						returnOfModel=newshops.getElementAt(i).toString();
//						System.out.println("value from list before split "+returnOfModel);
//					     index=returnOfModel.indexOf('-');
//						 firstOne=index+1;
//						 
//					     next=returnOfModel.substring(firstOne,returnOfModel.length()).trim();
//						 System.out.println("value from shoplist "+next);
//						 System.out.println("length is "+ lengthOfModel);
//						 
//						System.out.println("Selected value is:- "+selected);
//						System.out.println("array count is:"+i);
//					if(next.equalsIgnoreCase(selected)){
//						
//							//newshops.removeElement(returnOfModel);
//						removeElements.add(returnOfModel);
//				//	System.out.println("array count is:"+i);
////						val++;
//						}
//						
//					}
					
				
				
//check this code-------------------------------------------------------			
				
					
					shop_list.setModel(latestShop);
					
					
					while (itr.hasNext()) {
						ad_region_comboBox.insertItemAt(itr.next(), count);
						count++;
					}
					
					
					//update the region arraylist
					//set agency arraylist with the values in the list model
					
					
					int sizeOfArray=newRegionModel.getSize();
					
				    region.clear();
					
					for (int i = 0; i < sizeOfArray; i++) {
						System.out.println(newRegionModel.elementAt(i));
					region.add(newRegionModel.elementAt(i));
					
					}
					
					
					
				}
			}
		});

		// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
		ad_regionRemoveBtn.setBounds(389, 131, 86, 23);
		panel_5.add(ad_regionRemoveBtn);

		JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(233, 24, 242, 96);
		panel_5.add(scrollPane_1);
		region_list = new JList(modelRegion);
		scrollPane_1.setViewportView(region_list);
		region_list.setFont(new Font("Arial", Font.BOLD, 11));
		region_list.setBorder(new TitledBorder(null, "Region List", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		region_list.setBackground(UIManager.getColor("Button.highlight"));
		region_list.addListSelectionListener(listSelectionListenerRegion);

		JPanel panel_6 = new JPanel();
		panel_6.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		panel_6.setLayout(null);
		panel_6.setBounds(10, 365, 485, 224);
		addElementPanel.add(panel_6);

		JLabel label_28 = new JLabel("Add Shop");
		label_28.setFont(new Font("Tahoma", Font.PLAIN, 20));
		label_28.setBounds(10, 0, 126, 52);
		panel_6.add(label_28);

		JLabel lblShopName = new JLabel("Shop Name :");
		lblShopName.setBounds(10, 48, 77, 14);
		panel_6.add(lblShopName);

		ad_shop_textField = new JTextField();
		ad_shop_textField.setColumns(10);
		ad_shop_textField.setBounds(97, 45, 126, 20);
		panel_6.add(ad_shop_textField);

		ad_region_comboBox = new JComboBox();
		ad_region_comboBox.setBounds(97, 83, 126, 20);
		panel_6.add(ad_region_comboBox);

		JLabel lblRegion_2 = new JLabel("Region:");
		lblRegion_2.setBounds(10, 86, 77, 14);
		panel_6.add(lblRegion_2);

		JLabel lblAddress = new JLabel("Address:");
		lblAddress.setBounds(10, 123, 77, 14);
		panel_6.add(lblAddress);

		JLabel lblContDetails = new JLabel("Cont. Number:");
		lblContDetails.setBounds(10, 153, 77, 34);
		panel_6.add(lblContDetails);

		ad_contact_textfield = new JTextField();
		ad_contact_textfield.setColumns(10);
		ad_contact_textfield.setBounds(97, 160, 126, 20);
		panel_6.add(ad_contact_textfield);

		JButton ad_shopAddBtn = new JButton("Add");
		ad_shopAddBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< karama code
				// add new Shop to the System
				String newShop = ad_shop_textField.getText();
				String shopAddress = ad_shop_textArea.getText();
				String shopContactDetails = ad_contact_textfield.getText();
				String shop_region = (String) ad_region_comboBox
						.getSelectedItem();

				String listApperaringName="";
				
				System.out.print("region selected is#" + shop_region+"##");
				
				// check for null values before adding
				boolean updateDB=true;
				//Iterator itr = shop.iterator();
				ArrayList returnRegions=new ArrayList();
				//returnRegions=addElements.getRegionsMapToShop(newShop);
				
				returnRegions=addElements.getRegionIdAll(newShop);
				
				Iterator itr=returnRegions.iterator();
				
				
				if(!(newShop.trim().equals("")||shopAddress.trim().equals("")||shopContactDetails.trim().equals("")||shop_region==null)){
			
   // need to check weather shop name and region name get unique
					while (itr.hasNext()) {
						
						String returnVal=itr.next().toString();
						if(shop_region.equalsIgnoreCase(returnVal)){
							updateDB=false;
							
						}

					}
					
					
				if(updateDB){	
				DefaultListModel newShopModel = (DefaultListModel) shop_list
						.getModel();
				
				listApperaringName=newShop+"-"+shop_region;
				newShopModel.addElement(listApperaringName);
				shop_list.setModel(newShopModel);

				
				//System.out.print("shop selected is " + shop_region);
				System.out.print("region selected is#" + shop_region+"##");
				addElements.editShop(newShop, shop_region, shopAddress,
						shopContactDetails);
				clearFields();
				}
				
				else{
					
					JOptionPane.showMessageDialog(null, "Duplicate value for shop name and region"); 
					ad_shop_textField.setText("");	
					
					
				}
				
				
				
				
			}
				
				else{  
					JOptionPane.showMessageDialog(null, "Please Enter all the value in shop form Before Add"); 
					ad_shop_textField.setText("");	

				if (!(newShop.trim().equals("")
						|| shopAddress.trim().equals("")
						|| shopContactDetails.trim().equals("") || shop_region == null)) {

					DefaultListModel newShopModel = (DefaultListModel) shop_list
							.getModel();
					newShopModel.addElement(newShop);
					shop_list.setModel(newShopModel);

					// System.out.print("shop selected is " + shop_region);
					System.out
							.print("region selected is#" + shop_region + "##");
					addElements.editShop(newShop, shop_region, shopAddress,
							shopContactDetails);
					clearFields();

				}

					ad_shop_textArea.setText("");
					ad_contact_textfield.setText("");
					ad_region_comboBox.setSelectedIndex(-1);
				}
			}
		});

		// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
		ad_shopAddBtn.setBounds(163, 190, 62, 23);
		panel_6.add(ad_shopAddBtn);

		JButton ad_shopRemoveBtn = new JButton("Remove");
		ad_shopRemoveBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< karama code
				// check if selection value from the list is not null

				if (ValShop != null) {
					
					if(Common.updateTransactionsOnShopDelete(ValShop)){

					DefaultListModel newShopModel = (DefaultListModel) shop_list
							.getModel();
					newShopModel.removeElement(shop_list.getSelectedValue());
					shop_list.setModel(newShopModel);
					
					
					//split the string and send to db query
					
					int index=ValShop.indexOf('-');
					int firstOne=index+1;
					String shopOriginalName=ValShop.substring(0, index);
					String regionNameregardShop =ValShop.substring(firstOne, ValShop.length());

					//System.out.println("Values are "+shopOriginalName+" "+regionNameregardShop);
					addElements.removeShop(shopOriginalName.trim(),regionNameregardShop.trim());
					
					//update the shop arraylist
					//set shop arraylist with the values in the list model
					
					
					int sizeOfArray=newShopModel.getSize();
					
				    shop.clear();
					
					for (int i = 0; i < sizeOfArray; i++) {
						System.out.println(newShopModel.elementAt(i));
					region.add(newShopModel.elementAt(i));
					
					}
				
				}//if common
					else{
						JOptionPane.showMessageDialog(null, "You cannot delete this Shop - Unclosed Transactions "); 
						//ad_region_textField.setText("");	
					
						
					}

				}
			}
		});
		ad_shopRemoveBtn.setBounds(389, 185, 86, 23);
		panel_6.add(ad_shopRemoveBtn);

		modelShop = new DefaultListModel();
		ListSelectionListener listSelectionListenerShop = new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent listSelectionEvent) {
				boolean adjust = listSelectionEvent.getValueIsAdjusting();
				if (!adjust) {
					JList list = (JList) listSelectionEvent.getSource();
					int selections[] = list.getSelectedIndices();
					Object selectionValues[] = list.getSelectedValues();
					for (int i = 0, n = selections.length; i < n; i++) {
						if (i == 0) {
						}
						ValShop = (String) selectionValues[i];
						System.out.println(" Selections valShop: " + ValShop);
					}
				}
			}
		};

		JScrollPane scrollPane_2 = new JScrollPane();
		scrollPane_2.setBounds(233, 45, 242, 135);
		panel_6.add(scrollPane_2);
												shop_list = new JList(modelShop);
												scrollPane_2.setViewportView(shop_list);
												shop_list.setFont(new Font("Arial", Font.BOLD, 11));
												shop_list.setBorder(new TitledBorder(null, "Shop List", TitledBorder.LEADING, TitledBorder.TOP, null, null));
												
														shop_list.setBackground(UIManager.getColor("Button.highlight"));
														// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
														Border border1 = BorderFactory.createLineBorder(Color.BLACK);
														ad_shop_textArea = new JTextArea();
														ad_shop_textArea.setBorder(border);
														ad_shop_textArea.setBounds(97, 118, 126, 32);
														
														panel_6.add(ad_shop_textArea);
		shop_list.addListSelectionListener(listSelectionListenerShop);
		//>>>>>>>>>>>>>>>>>>>>>>>>>>>>CREDIT COLLECTION/REPORTS<<<<<<<<<<<<<<<<<
		JPanel creditCollectionPanel = new JPanel();
		tabbedPane.addTab("Reports", null, creditCollectionPanel, null);
		creditCollectionPanel.setLayout(null);

		JLabel label = new JLabel("Agency :");
		label.setBounds(109, 101, 46, 14);
		creditCollectionPanel.add(label);

		JLabel label_1 = new JLabel("Region :");
		label_1.setBounds(109, 168, 46, 14);
		creditCollectionPanel.add(label_1);

		JLabel label_2 = new JLabel("Shop :");
		label_2.setBounds(109, 246, 46, 14);
		creditCollectionPanel.add(label_2);

		JLabel label_3 = new JLabel("Date :");
		label_3.setBounds(109, 322, 46, 14);
		creditCollectionPanel.add(label_3);

		JLabel label_5 = new JLabel("Credit Collection");
		label_5.setFont(new Font("Tahoma", Font.PLAIN, 20));
		label_5.setBounds(10, 29, 163, 38);
		creditCollectionPanel.add(label_5);

		cc_agency_comboBox.setBounds(165, 98, 198, 20);
		ArrayList<Agency> agencies = Common.loadAgencies();
		cc_agency_comboBox.addItem("Choose Agency");
		for ( Agency agency: agencies ) {
			cc_agency_comboBox.addItem(agency);
		}
		creditCollectionPanel.add(cc_agency_comboBox);

		cc_region_comboBox.setBounds(165, 165, 198, 20);
		ArrayList<Region> cc_regions = Common.loadRegions();
		cc_region_comboBox.addItem("Choose Region");
		for ( Region region: cc_regions ) {
			cc_region_comboBox.addItem(region);
		}
		cc_region_comboBox.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				if ( cc_region_comboBox.getItemCount() > 0 ) {
					cc_shop_comboBox.removeAllItems();
					// System.out.println("Region Selected: "+cr_region_comboBox.getSelectedItem().toString());
					if (cc_region_comboBox.getSelectedItem().toString().equals("Choose Region")) {
						Common.shops = Common.loadShops("%");
	
					} else {
						Common.shops = Common.loadShops(cc_region_comboBox.getSelectedItem().toString());
					}
	
					cc_shop_comboBox.addItem("Choose Shop");
					for (Shop shop : Common.shops) {
						cc_shop_comboBox.addItem(shop);
					}
				}

			}

		});
		creditCollectionPanel.add(cc_region_comboBox);

		cc_shop_comboBox.setBounds(165, 243, 198, 20);
		if (Common.shops == null) {
			ArrayList<Shop> shops = new ArrayList<Shop>();
			shops = Common.loadShops("%");
			cc_shop_comboBox.addItem("Choose Shop");
			for (Shop shop : shops) {
				// System.out.println(shop);
				cc_shop_comboBox.addItem(shop);
			}
		}
		creditCollectionPanel.add(cc_shop_comboBox);

		JButton cc_submitBtn = new JButton("Submit");
		cc_submitBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if ( cc_agency_comboBox.getSelectedItem().toString().equals("Choose Agency") &&
						cc_shop_comboBox.getSelectedItem().toString().equals("Choose Shop") && 
						cc_region_comboBox.getSelectedItem().toString().equals("Choose Region") && 
						cc_date_textField.getText().equals("") ) {
					JOptionPane.showMessageDialog(null, "Choose at least one filter criteria");
				}
				else {
					HashMap<String,Object> params = new HashMap<String,Object>();
					if ( cc_agency_comboBox.getSelectedItem().toString().equals("Choose Agency") ) {
						params.put("agency", new Agency(-1,"%"));
					}
					else {
						params.put("agency", cc_agency_comboBox.getSelectedItem());
					}
					if ( cc_shop_comboBox.getSelectedItem().toString().equals("Choose Shop") ) {
						params.put("shop", new Shop(-1,"%"));
					}
					else {
						params.put("shop", cc_shop_comboBox.getSelectedItem());
					}
					if ( cc_date_textField.getText().equals("") ) {
						params.put("date", "%");
					}
					else {
						params.put("date", cc_date_textField.getText());
					}
					
					params.put("region", cc_region_comboBox.getSelectedItem());
					ArrayList<CreditElement> creditList = Reports.getCreditCollectionList(params);
					String column[] = { "Date", "Bill No", "Shop", "Region", "Agency",	"Total", "Remaining","                     ","                     ","                     " };
					try {
						new Printer(creditList,column,ViewGenerator.REPORT_TYPE_CREDIT_COLLECTION,cc_region_comboBox.getSelectedItem().toString()).setVisible(true);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					//new ViewGenerator(params,ViewGenerator.REPORT_TYPE_CREDIT_COLLECTION).setVisible(true);
				}
			}
		});
		cc_submitBtn.setBounds(274, 361, 89, 23);
		creditCollectionPanel.add(cc_submitBtn);

		cc_date_textField = new JTextField();
		cc_date_textField.setEditable(false);
		cc_date_textField.setColumns(10);
		cc_date_textField.setBounds(165, 314, 97, 20);
		creditCollectionPanel.add(cc_date_textField);

		JButton button_1 = new JButton("");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				cc_date_textField.setText(new DatePicker(home).setPickedDate());
			}
		});
		button_1.setIcon(new ImageIcon(HomeScreen.class
				.getResource("/com/midi/repnet/resources/date.gif")));
		button_1.setBounds(272, 313, 21, 23);
		creditCollectionPanel.add(button_1);

		JSeparator separator_1 = new JSeparator();
		separator_1.setBounds(10, 412, 486, 2);
		creditCollectionPanel.add(separator_1);
		
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Credit Details", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel.setBounds(20, 435, 208, 143);
		creditCollectionPanel.add(panel);
		panel.setLayout(null);
		
				JButton button = new JButton("");
				button.setBounds(169, 28, 21, 23);
				panel.add(button);
				button.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						cc_dateChequeTextField.setText(new DatePicker(home)
								.setPickedDate());
					}
				});
				button.setIcon(new ImageIcon(HomeScreen.class
						.getResource("/com/midi/repnet/resources/date.png")));
				
						cc_dateChequeTextField = new JTextField();
						cc_dateChequeTextField.setBounds(66, 29, 97, 22);
						panel.add(cc_dateChequeTextField);
						cc_dateChequeTextField.setEditable(false);
						cc_dateChequeTextField.setColumns(10);
						
								JLabel label_9 = new JLabel("Date :");
								label_9.setBounds(28, 28, 46, 14);
								panel.add(label_9);
								
										/////////////////////////////////  Find Credit overdue Auther Prabodha
										JButton btnNewButton_1 = new JButton("Credit Overdue");
										btnNewButton_1.setBounds(66, 62, 124, 23);
										panel.add(btnNewButton_1);
										
										JButton credit_paidSubmit = new JButton("Credit Paid");
										credit_paidSubmit.setBounds(66, 96, 124, 23);
										panel.add(credit_paidSubmit);
												
												JPanel panel_1 = new JPanel();
												panel_1.setBorder(new TitledBorder(null, "Cheque Details", TitledBorder.LEADING, TitledBorder.TOP, null, null));
												panel_1.setBounds(274, 435, 208, 143);
												creditCollectionPanel.add(panel_1);
												panel_1.setLayout(null);
												
														JButton cc_dateCheque = new JButton("Date Cheques");
														cc_dateCheque.setBounds(41, 76, 124, 42);
														panel_1.add(cc_dateCheque);
														
														JLabel lblClickHereTo = new JLabel("Click here to view the cheque details");
														lblClickHereTo.setHorizontalAlignment(SwingConstants.CENTER);
														lblClickHereTo.setBounds(10, 21, 188, 60);
														panel_1.add(lblClickHereTo);
												cc_dateCheque.addActionListener(new ActionListener() {
													public void actionPerformed(ActionEvent e) {
														Calendar currentDate = Calendar.getInstance();
												        SimpleDateFormat formatter= 
												        new SimpleDateFormat("yyyy-MM-dd");
												        String date = formatter.format(currentDate.getTime());
														//String date = cc_dateChequeTextField.getText();
														HashMap<String,Object> params = new HashMap<String,Object>();
														params.put("date", date);
//														if(date.equals("")){
//															JOptionPane.showMessageDialog(null, "Please enter the Date..!");
//														}else{
															new ViewGenerator(params,ViewGenerator.REPORT_TYPE_CHEQUE_RESULTS).setVisible(true);
														//}				
													}
												});
										credit_paidSubmit.addActionListener(new ActionListener() {
											public void actionPerformed(ActionEvent arg0) {
												String date = cc_dateChequeTextField.getText();
												HashMap<String, Object> params = new HashMap<String,Object>();
												params.put("date", date);
												if(date.equals("")){
													JOptionPane.showMessageDialog(null, "Please enter the Date..!");
												}else{
													new ViewGenerator(params, ViewGenerator.REPORT_TYPE_CREDIT_PAID).setVisible(true);
												}
												
											}
										});
										btnNewButton_1.addActionListener(new ActionListener() {
											public void actionPerformed(ActionEvent arg0) {
												String date = cc_dateChequeTextField.getText();
												HashMap<String,Object> params = new HashMap<String,Object>();
												params.put("date", date);
												if(date.equals("")){
													JOptionPane.showMessageDialog(null, "Please enter the Date..!");
												}else{
													new ViewGenerator(params,ViewGenerator.REPORT_TYPE_CREDIT_OVERDUE).setVisible(true);
												}
												
											}
										});
		
		JLabel lblcopyMidiSolutions = new JLabel("\u00a9 MiDi Solutions");
		lblcopyMidiSolutions.setForeground(Color.LIGHT_GRAY);
		lblcopyMidiSolutions.setBounds(438, 642, 83, 14);
		contentPane.add(lblcopyMidiSolutions);
	}

	private static void clearFields() {

		ad_agency_textField.setText("");
		ad_region_textField.setText("");
		ad_shop_textField.setText("");
		ad_shop_textArea.setText("");
		ad_contact_textfield.setText("");
		ad_region_comboBox.setSelectedIndex(-1);

	}
	private void clearCreditRemittanceFields() {
		cr_amount_textField.setText("");
		cr_bank_textfield.setText("");
		cr_chequeAmount_textField.setText("");
		cr_chequeNo_textField.setText("");
		cr_date_textField.setText("");
		this.cr_agencycomboBox.setSelectedIndex(0);
		this.cr_billNo_comboBox.setSelectedIndex(0);
		this.cr_region_comboBox.setSelectedIndex(0);
		this.cr_shop_comboBox.setSelectedIndex(0);
		
	}
}
